/*
 * VHLocaleResolver.java
 * Created on 7 oct. 2006
 *
 * VirtualHockey.net Internet Platform
 * Copyright (c) 2005-2006, Jan Janke
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.server.servlet.base;

import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AbstractLocaleResolver;

/**
 * <p>
 * Standard {@link LocaleResolver} of the VH application. The locale to use is read from
 * the HTTP accept-language header. If the requested language is supported, the resolved
 * locale will use the specified language. Otherwise, the default locale is used as
 * fallback.
 * </p>
 *
 * <p>
 * The {@link Locale} that is returned by the {@link #resolveLocale(HttpServletRequest)}
 * method always uses the country of the default locale. Only the language is changed.
 * Example: If the default locale is 'de-CH' and the client that connects uses 'fr-FR',
 * the resolved locale will be 'fr-CH' (if French is an accepted language, see
 * {@link #setAcceptedLanguages(Set)} for details).
 * </p>
 *
 * @version $Id: VHLocaleResolver.java 40 2006-10-08 15:36:13Z janjanke $
 * @author jjanke
 */
public class VHLocaleResolver extends AbstractLocaleResolver
{
  /*
   * ================================================================================
   * Private members
   * ================================================================================
   */
  private Map<String,Locale> d_mapAcceptedLanguages;

  /*
   * ================================================================================
   * Non-static methods
   * ================================================================================
   */

  /**
   * Sets the accepted languages. This LocaleResolver only accepts locales that refer to
   * an accepted language. If a request refers to a non-accepted language, the default
   * language will automatically be used.
   *
   * @param setLanguages a set containing the officile ISO two letter abbreviations for
   *        all languages that are supported by the application
   */
  public void setAcceptedLanguages( Map<String,Locale> mapAcceptedLanguages )
  {
    d_mapAcceptedLanguages = mapAcceptedLanguages;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.springframework.web.servlet.LocaleResolver#resolveLocale(javax.servlet.http.HttpServletRequest)
   */
  public Locale resolveLocale( HttpServletRequest rq )
  {
    Locale locale = d_mapAcceptedLanguages.get( rq.getLocale().getLanguage() );

    if ( locale == null )
      locale = getDefaultLocale();

    return locale;
  }

  /**
   * Resolves the locale from the given locale string.
   *
   * @param strLocale the locale string (e.g. 'de', 'de_CH' or 'fr_FR_UNIX' etc.)
   * @return the matching locale object
   */
  public Locale resolveLocale( final String strLocale )
  {
    String strRequestedLanguage;

    if ( strLocale.equals( "default" ) )
      return getDefaultLocale();

    if ( strLocale.length() == 2 )
      strRequestedLanguage = strLocale;
    else
      strRequestedLanguage = strLocale.substring( 0, strLocale.indexOf( '_' ) );

    Locale locale = d_mapAcceptedLanguages.get( strRequestedLanguage );

    if ( locale == null )
      locale = getDefaultLocale();

    return locale;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.springframework.web.servlet.LocaleResolver#setLocale(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse, java.util.Locale)
   */
  public void setLocale( HttpServletRequest rq, HttpServletResponse rp, Locale locale )
  {
    throw new UnsupportedOperationException( "Changing the locale dynamically is not yet supported." );
  }

}
